//=============================================================================
// Window_SkillList
//=============================================================================
const originSkillListInit = Window_SkillList.prototype.initialize;
Window_SkillList.prototype.initialize = function (x, y, width, height) {
    originSkillListInit.call(this, x, y, width, height);
    this._backgroundSprite = new Sprite();
    this._backgroundSprite.bitmap =
        ImageManager.loadBitmap('img/system/UI/', 'MenuBg_List');
    this.addChildAt(this._backgroundSprite, 0);
    this._backgroundSprite.x = -17;
    this._backgroundSprite.y = -1;
};


// changeWindowWidth
Window_SkillList.prototype.windowWidth = function () {
    return 128;
};

Window_SkillList.prototype.maxCols = function () {
    return 1;
};

/*
const isCurrentItemEnabled = Window_SkillList.prototype.isCurrentItemEnabled
Window_SkillList.prototype.isCurrentItemEnabled = function () {
    if (!$gameParty.canUse(this.item())) return false

    return isCurrentItemEnabled.call(this)
}
*/
const originSkillListSetActor = Window_SkillList.prototype.setActor;
Window_SkillList.prototype.setActor = function(actor) {
    originSkillListSetActor.call(this,actor);
    this.selectLast();
};

Window_SkillList.prototype.drawIcon = function (iconIndex, x, y, alpha = 255) {
    const bitmap = ImageManager.loadSystem('IconSet');
    const pw = Window_Base._iconWidth;
    const ph = Window_Base._iconHeight;
    const sx = iconIndex % 16 * pw;
    const sy = Math.floor(iconIndex / 16) * ph;

    // 应用带透明度的icon
    const opacityBitmap = bitmap.clone(bit => bit.paintOpacity = alpha)
    this.contents.blt(opacityBitmap, sx, sy, pw, ph, x, y)
}

// 老饕：覆写可用状态（YEP默认物品全都可以使用）
/*
Window_SkillList.prototype.isEnabled = function (item) {
    if (!item) return false;
    return Yanfly.Item.Window_SkillList_isEnabled.call(this, item);
};
*/

// 不可用项以红色显示（老饕：因此方法影响装备窗口绘制，故将可用条件判定方法修改为isEnabled，可通用）
Window_SkillList.prototype.drawItemName = function (item, x, y, width) {
    width = width || 312
    if (item) {
        const iconBoxWidth = Window_Base._iconWidth + 4;
        this.resetTextColor();
        //if (!$gameParty.canUse(item)) {
        if (!this.isEnabled(item)){
            this.changeTextColor(this.hpGaugeColor1());
            this.drawIcon(item.iconIndex, x + 2, y + 2, 125);//alpha=125
        } else {
            this.drawIcon(item.iconIndex, x + 2, y + 2);
        }
        this.drawText(item.name, x + iconBoxWidth, y, width - iconBoxWidth);
    }
}

// 补充炼符界面
Window_SkillList.prototype.selectLast = function() {
    if(ConfigManager.commandRemember){
        let skill= new Game_Item();
        if ($gameParty.inBattle()) {
            skill = this._actor.lastBattleSkill();
        } else if (SceneManager._scene instanceof Scene_Fu) {
            skill = this._actor.lastMenuFu();
        } else {
            skill = this._actor.lastMenuSkill();
        }
        var index = this._data.indexOf(skill);
        this.select(index >= 0 ? index : 0);
    }else{
        this.select(0);
    }

};